package com.banmao.mall.service.admin.impl;

import com.banmao.mall.mapper.GoodsMapper;
import com.banmao.mall.mapper.OrderMapper;
import com.banmao.mall.mapper.UserMapper;
import com.banmao.mall.model.bo.admin.statistic.OrderTotalAndAmountByDay;
import com.banmao.mall.model.vo.BaseRespVO;
import com.banmao.mall.model.vo.admin.statistic.*;
import com.banmao.mall.service.admin.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class StatisticServiceImpl implements StatisticService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<UserTotalRowsVO> statisticsUsers() {
        List<UserTotalRowsVO> userTotalRowsVOS = userMapper.totalNumberOfUsers();
        return userTotalRowsVOS;
    }

    @Override
    public List<OrderTotalRowsVO> statisticsOrders() {
//        1.根据日期获取订单数量日期信息
//        2.根据日期获取订单的人数
//        3.计算该天人均消费pcr
//        4.封装数据
        List<OrderTotalRowsVO> list = new ArrayList<OrderTotalRowsVO>();
        List<OrderTotalAndAmountByDay> orderTotalAndAmountByDays = orderMapper.orderTotalAndAmountByDay();
        for (OrderTotalAndAmountByDay orders : orderTotalAndAmountByDays) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//            System.out.println(df.format(date));
            Integer integer = orderMapper.orderTotalCustomersNumOfDay(df.format(orders.getDay()));

            OrderTotalRowsVO orderTotalRowsVO = new OrderTotalRowsVO();
            orderTotalRowsVO.setAmount(orders.getAmount());
            orderTotalRowsVO.setOrders(orders.getOrders());
            orderTotalRowsVO.setDay(orders.getDay());

            orderTotalRowsVO.setCustomers(integer);
            int i=(int)(orders.getAmount()/integer*100);
            orderTotalRowsVO.setPcr(i/100.0);

            list.add(orderTotalRowsVO);
        }
        return list;
    }

    @Override
    public List<GoodsTotalRowsVO> statisticsGoods() {
//        1.根据日期分组获取订单的日出单量和总的金额
//        2.根据对应的的日期查找当日货品的数量
//        3.封装
        List<GoodsTotalRowsVO> list = new ArrayList<>();
        List<OrderTotalAndAmountByDay> orderTotalAndAmountByDays = orderMapper.orderTotalAndAmountByDay();
        for (OrderTotalAndAmountByDay orders: orderTotalAndAmountByDays) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Integer integer = goodsMapper.goodsTotalNumOfDay(df.format(orders.getDay()));
            GoodsTotalRowsVO goodsTotalRowsVO = new GoodsTotalRowsVO();
            goodsTotalRowsVO.setAmount(orders.getAmount());
            goodsTotalRowsVO.setDay(orders.getDay());
            goodsTotalRowsVO.setOrders(orders.getOrders());
            goodsTotalRowsVO.setProducts(integer);
            list.add(goodsTotalRowsVO);
        }
        return list;
    }
}
